package com.fuxue.controller.server;
import com.fuxue.dto.*;
import com.fuxue.entity.AllFiled;
import com.fuxue.entity.Server;
import com.fuxue.constant.JwtClaimsConstant;
import com.fuxue.properties.JwtProperties;
import com.fuxue.result.PageResult;
import com.fuxue.result.Result;
import com.fuxue.service.ServerService;
import com.fuxue.utils.JwtUtil;
import com.fuxue.vo.ServerLoginVO;
import com.fuxue.vo.ServerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users/server")
@Slf4j
@Api(tags = "服务者相关接口")
public class ServerController {
    @Autowired
    private ServerService serverService;

    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    @CrossOrigin
    @ApiOperation(value = "服务者登录")
    public Result<ServerLoginVO> login(@RequestBody ServerLoginDTO serverLoginDTO) {
        log.info("服务者登录：{}", serverLoginDTO);

        Server server = serverService.login(serverLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, server.getServerId());
        System.out.println(claims);
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        List<String>filed = new ArrayList<String>();
        filed = serverService.getFiled(server.getServerId());

        ServerLoginVO serverLoginVO = ServerLoginVO.builder()
                .serverId(server.getServerId())
                .serverName(server.getServerName())
                .avatar(server.getAvatar())
                .filed(filed)
                .token(token)
                .build();

        return Result.success(serverLoginVO);
    }

    @PostMapping("/logout")
    @CrossOrigin
    @ApiOperation("服务者退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增服务者
     * @param
     * @return
     */
    @PostMapping("/new")
    @ApiOperation("新增服务者")
    public Result save(@RequestBody ServerDTO serverDTO){
         log.info("新增服务者：{}",serverDTO);
         serverService.save(serverDTO);
         return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询服务者信息")
    public Result<ServerVO> getById(@PathVariable Integer id) {
        log.info("根据id查询员工信息，{}", id);
        ServerVO serverVO = serverService.getById(id);
        return Result.success(serverVO);
    }
    @PutMapping("/filed")
    @ApiOperation("修改领域")
    public Result updateName(@RequestBody ServerUPFiledDTO serverUPFiledDTO) {
        log.info("修改服务者负责领域：{}", serverUPFiledDTO);
        serverService.updateWithFiled(serverUPFiledDTO);

        //将所有的菜品缓存数据清理掉，所有以dish_开头的key

        return Result.success();
    }
  @PutMapping("/name")
  @ApiOperation("修改服务者用户名")
    public Result updateName(@RequestBody ServerUpNameDTO serverUpNameDTO)
    {
        log.info("修改服务者用户名：{}", serverUpNameDTO);
        serverService.updateWithName(serverUpNameDTO);

        //将所有的菜品缓存数据清理掉，所有以dish_开头的key

        return Result.success();
    }
    @PutMapping("/password")
    @ApiOperation("修改密码")
    public Result updatePassword(@RequestBody ServerUpPasswordDTO serverUpPasswordDTO)
    {
        log.info("修改服务者密码：{}",serverUpPasswordDTO);
        serverService.updateWithPassword(serverUpPasswordDTO);
        return Result.success();
    }
    @PutMapping("/image")
    @ApiOperation("修改头像")
    public Result updateImage(@RequestBody ServerUpAdvateDTO serverUpAdvateDTO)
    {
        log.info("修改服务者头像：{}",serverUpAdvateDTO);
        serverService.updateWithAdvate(serverUpAdvateDTO);
        return Result.success();
    }
    @GetMapping("/AllFiled")
    @CrossOrigin
    @ApiOperation("得到全部领域")
    public Result<List<AllFiled>>getFiled()
    {
        log.info("得到全部领域");
        List<AllFiled>list = serverService.getAllFiled();


        return Result.success(list);
    }




}
